package com.tucc.hello.leetcode.no1_400.no1_200.no41_60.no50;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        double x = 2.00000;
        int n = -2147483648;
        System.out.println(solution.myPow(x, n));
        System.out.println(Math.pow(x, n));
    }

    public double myPow(double x, int n) {
        if (x == 0) {
            return 0;
        }
        if (x == 1) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        if (n == 0) {
            return 1;
        }
        if (n < 0) {
            if (n == Integer.MIN_VALUE) {
                return 1 / x * myPow(1 / x, Integer.MAX_VALUE);
            }
            return myPow(1 / x, -n);
        }
        double half = myPow(x, n / 2);
        if (n % 2 == 1) {
            return half * half * x;
        }
        return half * half;
    }
}
